clear all
global PATH  ""
global DIR "$PATH\tables_main"
global OUTPUT "$PATH\figures_main"

* set type of figure to generate
local figext "tif"
 

********************************************************************************
**  Main Text - Fig 1
********************************************************************************
* Panels A-C created internally at on IRS servers
* Panel D created in Excel (fig1_panelD.xls)


********************************************************************************
**  Main - Fig 2 
********************************************************************************
local sp "2"
******************
**  Panel A - trends
******************
import delimited "$DIR\figure2_panelA_ctrl.csv", rowrange(28:51) colrange(:2) clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
}

drop if year == .
keep year coeff* ste*

**For base 2012
set obs `=_N+1'
replace year = 2012 if year == .
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

ren coeff`sp' coeffctrl 
drop ste* upper* lower*

sort year
tempfile tctrl
save `tctrl', replace



******************
**  Panels B and D - ITT and 2sls DD
******************
*set number of specs in table 
local ss = 4
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure2_panelB.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
}

drop if year == .

keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1
gen tt = "itt"

tempfile titt
save `titt', replace


******************
**  Panel D - 2sls DD
******************				
*set number of specs in table 
local ss = 4
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure2_panelD.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
}

drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "iv"

append using `titt'

local lcolor1 "navy"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "4"
local spec1_1 = `zspec1_1'+1

local gtype "scatter"
**
* Event study figure 
**	
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(gs8) ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig2_panelB.`figext', replace 
	
/* black and white version */
local lcolor1 "gs4"
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(gs8) ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig2_panelB_bw.`figext', replace 
			
	

/* Panel D */			
local lcolor1 "navy"
local lcolor2 "cranberry"
local ms1 "o"
local ms2 "t"
local ymin = -.03
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "4"
local spec1_1 = `zspec1_1'+1
local zspec2 "4"
local spec2 = `zspec2'+1


local gtype "scatter"
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'3  if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(gs8)  ) ///
			(`gtype' coeff`spec1_1' `xvar'3 if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(`ms1') msize(medium) mcolor(`lcolor1') ) ///
			 (rcap lower`spec2' upper`spec2' `xvar'1  if year>=`xvarmin' & year<=`xvarmax' & tt=="iv",  lcolor(gs8) lpattern(dash) ) ///
			(`gtype' coeff`spec2' `xvar'1 if year>=`xvarmin' & year<=`xvarmax' & tt=="iv" ,  lcolor(`lcolor2') lwidth(medium) msymbol(`ms2') msize(medium) mcolor(`lcolor2') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))    ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' 2016.5)) 	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))  /// 
			legend(off) ///
			xtitle("year") 	 ylab(`ymin'(`ygap')`ymax' /*, nogrid*/)   ///
			text(-0.019 2016.3 "ITT", place(e) color(navy) size(medium))  ///	
			text(-0.0255 2016.3 "2sls", place(e) color(sienna) size(medium)) 	
graph export $OUTPUT/fig2_panelD.`figext', replace 


/* black and white version */
local lcolor1 "gs3"
local lcolor2 "gs7"

local gtype "scatter"
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'3  if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(gs5)  ) ///
			(`gtype' coeff`spec1_1' `xvar'3 if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(`ms1') msize(medium) mcolor(`lcolor1') ) ///
			 (rcap lower`spec2' upper`spec2' `xvar'1  if year>=`xvarmin' & year<=`xvarmax' & tt=="iv",  lcolor(gs7) lpattern(dash) ) ///
			(`gtype' coeff`spec2' `xvar'1 if year>=`xvarmin' & year<=`xvarmax' & tt=="iv" ,  lcolor(`lcolor2') lwidth(medium) msymbol(`ms2') msize(medium) mcolor(`lcolor2') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))    ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' 2016.5)) 	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))  /// 
			legend(off) ///
			xtitle("year") 	 ylab(`ymin'(`ygap')`ymax' /*, nogrid*/)   ///
			text(-0.019 2016.3 "ITT", place(e) color(`lcolor1') size(medium))  ///	
			text(-0.0255 2016.3 "2sls", place(e) color(`lcolor2') size(medium)) 	
graph export $OUTPUT/fig2_panelD_bw.`figext', replace 



*****************
** panel A
*****************
sort year 
merge m:1 year using `tctrl'
drop _merge

local spec "4"
gen coefftreat = coeff`spec'+coeffctrl

local lcolor1 "navy"
local ms1 "t"
local ms2 "o"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

graph twoway (connected coeffctrl `xvar'  if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(gs8) lwidth(medium) msymbol(`ms1') msize(medium) mcolor(gs8) lpattern(dash) ) ///
			(connected coefftreat `xvar' if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(`ms2') msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "exposed") label(1 "unexposed") order(2 1))			///
			xtitle("year") 	
graph export $OUTPUT/fig2_panelA.`figext', replace 


/* black and white version */
local lcolor1 "gs3"
local lcolor2 "gs7"

graph twoway (connected coeffctrl `xvar'  if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(gs8) lwidth(medium) msymbol(`ms1') msize(medium) mcolor(gs8) lpattern(dash) ) ///
			(connected coefftreat `xvar' if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(`ms2') msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "exposed") label(1 "unexposed") order(2 1))			///
			xtitle("year") 	
graph export $OUTPUT/fig2_panelA_bw.`figext', replace 



******************
**  Panel C - first stage
******************
import delimited "$DIR\figure2_panelC.csv", clear

local ms1 "t"
local ms2 "o"

* color
local tt "tt1112"
graph twoway (connected sh year if trt==1 & tvc=="`tt'", mcolor(navy) lcolor(navy) lpattern(solid) msymbol(`ms2')) /// 
			(connected sh year if trt==0 & tvc=="`tt'", mcolor(cranberry) lcolor(cranberry)  lpattern(dash) msymbol(`ms1')), /// 
			graphregion(fcolor(white) icolor(white) color(white))  plotregion(margin(medlarge)) ///
			ytitle("Fraction Treated") ///
			xline(2012.3, lcolor(black) lpattern(dash)) legend(col(2) order(1 "Basline Exposed Firms"  2 "Baseline Unexposed Firms"))
graph export "$OUTPUT/fig2_panelC.`figext'", replace


* B&W version
local tt "tt1112"
graph twoway (connected sh year if trt==1 & tvc=="`tt'", mcolor(gs3) lcolor(gs3) lpattern(solid) msymbol(`ms2')) /// 
			(connected sh year if trt==0 & tvc=="`tt'", mcolor(gs7) lcolor(gs7)  lpattern(dash) msymbol(`ms1')), /// 
			graphregion(fcolor(white) icolor(white) color(white))  plotregion(margin(medlarge)) ///
			ytitle("Fraction Treated") ///
			xline(2012.3, lcolor(black) lpattern(dash)) legend(col(2) order(1 "Basline Exposed Firms"  2 "Baseline Unexposed Firms"))
graph export "$OUTPUT/fig2_panelC_bw.`figext'", replace




	
********************************************************************************
********************************************************************************
********************************************************************************
**  Fig 3 - Panel A - Employment - trends
********************************************************************************
********************************************************************************
********************************************************************************
*set number of specs in table 
local ss = 1
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure3_PanelA.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
}

drop if year == .

keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "itt"

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.04
local ymax = .04
local ygap = 0.02
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "1"
local spec1_1 = `zspec1_1'+1

local gtype "scatter"
**
* Event study figure 
**
/* Panel A */
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 	ylab(`ymin'(`ygap')`ymax' /*, nogrid*/)	 yscale(range(`ymin'1 `ymax'1))
graph export $OUTPUT/fig3_panelA.`figext', replace
		
/* black and white version */
local lcolor1 "gs4"
local secolor1 "gs8"

graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 	ylab(`ymin'(`ygap')`ymax' /*, nogrid*/)	 yscale(range(`ymin'1 `ymax'1))
graph export $OUTPUT/fig3_panelA_bw.`figext', replace



*******************************************************
**  Fig 3 - Panel B - Employment - decomp
*******************************************************

*set number of specs in table 
local ss = 3
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure3_PanelB.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
}

drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.2
gen year3 = year-.2

gen tt = "itt"

gen yearp=year+.13
gen yearm=year-.13
			
local lcolor1 "gs8"
local lcolor2 "cranberry"
local lcolor3 "navy"
local ms1 "o"
local ms2 "s"
local ms3 "t"
local ymin = -.04
local ymax = .04
local ygap = 0.02
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "2"
local spec1 = `zspec1_1'
local spec2 = `zspec1_1'+1
local spec3 = `zspec1_1'+2
gen scoeff`spec3'=-coeff`spec3'
gen supper`spec3' = scoeff`spec3' + 1.96*ste`spec3'
gen slower`spec3' = scoeff`spec3' - 1.96*ste`spec3'

local gtype "connected"
	graph twoway (rcap lower`spec1' upper`spec1' year if year>=2008 & year<=2016,  lcolor(`lcolor1'%60) lpattern(solid) ) ///
			(`gtype' coeff`spec1' year if year>=2008 & year<=2016,  lcolor(`lcolor1') lwidth(medium) msymbol(`ms1') msize(small) mcolor(`lcolor1') lpattern(solid) ) ///
			(rcap lower`spec2' upper`spec2' yearm if year>=2008 & year<=2016,  lcolor(`lcolor2'%60) lpattern(shortdash) ) ///
			(`gtype' coeff`spec2' yearm if year>=2008 & year<=2016,  lcolor(`lcolor2') lwidth(medium) msymbol(`ms2') msize(small) mcolor(`lcolor2') lpattern(shortdash) ) ///
			(rcap slower`spec3' supper`spec3' yearp if year>=2008 & year<=2016,  lcolor(`lcolor3'%60) lpattern(dash) ) ///
			(`gtype' scoeff`spec3' yearp if year>=2008 & year<=2016,  lcolor(`lcolor3') lwidth(medium) msymbol(`ms3') msize(small) mcolor(`lcolor3') lpattern(dash) ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(2008 (1) 2016 ) xscale(range(2008 2016)) yscale(range(-2.5 2.5)) ylabel(-2 (1) 2)	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))	                          ///
			legend(label(2 "employment") label(4 "entrants") label(6 "separators")  order(2 4 6) col(3))			///
			xtitle("year")	
graph export $OUTPUT/fig3_PanelB.`figext', replace


/* black and white version */
local lcolor1 "gs9"
local lcolor2 "gs6"
local lcolor3 "gs2"

local gtype "connected"
	graph twoway (rcap lower`spec1' upper`spec1' year if year>=2008 & year<=2016,  lcolor(`lcolor1') lpattern(solid) ) ///
			(`gtype' coeff`spec1' year if year>=2008 & year<=2016,  lcolor(`lcolor1') lwidth(medium) msymbol(`ms1') msize(small) mcolor(`lcolor1') lpattern(solid) ) ///
			(rcap lower`spec2' upper`spec2' yearm if year>=2008 & year<=2016,  lcolor(`lcolor2') lpattern(shortdash) ) ///
			(`gtype' coeff`spec2' yearm if year>=2008 & year<=2016,  lcolor(`lcolor2') lwidth(medium) msymbol(`ms2') msize(small) mcolor(`lcolor2') lpattern(shortdash) ) ///
			(rcap slower`spec3' supper`spec3' yearp if year>=2008 & year<=2016,  lcolor(`lcolor3') lpattern(dash) ) ///
			(`gtype' scoeff`spec3' yearp if year>=2008 & year<=2016,  lcolor(`lcolor3') lwidth(medium) msymbol(`ms3') msize(small) mcolor(`lcolor3') lpattern(dash) ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(2008 (1) 2016 ) xscale(range(2008 2016)) yscale(range(2.5 2.5)) ylabel(-2 (1) 2)	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))	                          ///
			legend(label(2 "employment") label(4 "entrants") label(6 "separators")  order(2 4 6) col(3))			///
			xtitle("year")	
graph export $OUTPUT/fig3_PanelB_bw.`figext', replace


			
			
			
********************************************************************************
********************************************************************************
********************************************************************************
**  Fig 4 - Firm trends
********************************************************************************
********************************************************************************
********************************************************************************
**
** Valadd per worker
**
*set number of specs in table 
local ss = 1
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 27

* import csv file and select first column only 
import delimited "$DIR\figure4_valaddpw.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
	}

drop if year == .

keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen inc = "valaddpw"
tempfile tva
save `tva', replace

**
** Non-wage deductions
**
*set number of specs in table 
local ss = 1
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 27

* import csv file and select first column only 
import delimited "$DIR\figure4_ded_nonwage.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
}

drop if year == .

keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen inc = "deductions"
tempfile tded
save `tded', replace

**
** Revenue
**
*set number of specs in table 
local ss = 1
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 27

* import csv file and select first column only 
import delimited "$DIR\figure4_revenue.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.

	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
}

drop if year == .

keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen inc = "revenue"
append using `tva'
append using `tded'

drop if year==2011 & coeff2==0

gen zero =0
gen year1 = year
gen year2 = year+.2
gen year3 = year-.2
gen tt = "itt"
gen yearp=year+.13
gen yearm=year-.13

local ms1 "s"
local ms2 "o"
local ms3 "t"
local ymin = -.03
local ymax = .03
local ygap = 0.02
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1 "1"
local zspec2 "1"
local zspec3 "1"
local spec1 = `zspec1'+1
local spec2 = `zspec2'+1
local spec3 = `zspec3'+1

local gtype "connected"

**
* Event study figure 
**
local lcolor1 "navy"
local lcolor2 "gs8"
local lcolor3 "cranberry"

	graph twoway (rcap lower`spec1' upper`spec1' yearm if year>=2008 & year<=2016 & inc=="valaddpw",  lcolor(`lcolor1'%60) lpattern(solid) ) ///
			(`gtype' coeff`spec1' yearm if year>=2008 & year<=2016 & inc=="valaddpw",  lcolor(`lcolor1') lwidth(medium) msymbol(`ms1') msize(small) mcolor(`lcolor1')  lpattern(solid) ) ///
			(rcap lower`spec2' upper`spec2' year if year>=2008 & year<=2016 & inc=="deductions",  lcolor(`lcolor2'%60)  lpattern(shortdash) ) ///
			(`gtype' coeff`spec2' year if year>=2008 & year<=2016 & inc=="deductions",  lcolor(`lcolor2') lwidth(medium) msymbol(`ms2') msize(small) mcolor(`lcolor2')  lpattern(shortdash)) ///
			(rcap lower`spec3' upper`spec3' yearp if year>=2008 & year<=2016 & inc=="revenue",  lcolor(`lcolor3'%60) lpattern(dash) ) ///
			(`gtype' coeff`spec3' yearp if year>=2008 & year<=2016 & inc=="revenue",  lcolor(`lcolor3') lwidth(medium) msymbol(`ms3') msize(small) mcolor(`lcolor3')  lpattern(dash)), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///	                          ///
			legend(label(4 "non-wage deductions") label(2 "value added/worker") label(6 "revenue")  order(6 4 2) col(1))			///
			xtitle("year")		ylab(`ymin'(`ygap')`ymax' /*, nogrid*/)	 yscale(range(`ymin'1 `ymax'1))
graph export $OUTPUT/fig4_PanelA.`figext', replace
			

			
/* black and white version */
local lcolor1 "gs6"
local lcolor2 "gs9"
local lcolor3 "gs2"

	graph twoway (rcap lower`spec1' upper`spec1' yearm if year>=2008 & year<=2016 & inc=="valaddpw",  lcolor(`lcolor1'%60) lpattern(solid) ) ///
			(`gtype' coeff`spec1' yearm if year>=2008 & year<=2016 & inc=="valaddpw",  lcolor(`lcolor1') lwidth(medium) msymbol(`ms1') msize(small) mcolor(`lcolor1')  lpattern(solid) ) ///
			(rcap lower`spec2' upper`spec2' year if year>=2008 & year<=2016 & inc=="deductions",  lcolor(`lcolor2'%60)  lpattern(shortdash) ) ///
			(`gtype' coeff`spec2' year if year>=2008 & year<=2016 & inc=="deductions",  lcolor(`lcolor2') lwidth(medium) msymbol(`ms2') msize(small) mcolor(`lcolor2')  lpattern(shortdash)) ///
			(rcap lower`spec3' upper`spec3' yearp if year>=2008 & year<=2016 & inc=="revenue",  lcolor(`lcolor3'%60) lpattern(dash) ) ///
			(`gtype' coeff`spec3' yearp if year>=2008 & year<=2016 & inc=="revenue",  lcolor(`lcolor3') lwidth(medium) msymbol(`ms3') msize(small) mcolor(`lcolor3')  lpattern(dash)), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///	                          ///
			legend(label(4 "non-wage deductions") label(2 "value added/worker") label(6 "revenue")  order(6 4 2) col(1))			///
			xtitle("year")		ylab(`ymin'(`ygap')`ymax' /*, nogrid*/)	 yscale(range(`ymin'1 `ymax'1))
graph export $OUTPUT/fig4_PanelA_bw.`figext', replace





********************************************************************************
********************************************************************************
********************************************************************************
**  Fig 5 - Earnings Decomposition
********************************************************************************
********************************************************************************
********************************************************************************
**************
**  Panel A - decomposition of earnings response
**************
import delimited $DIR\figure5_panelA.csv, rowrange(4:6) colrange(2:11) clear

gen year=2016

foreach i of numlist 1/10 {
	local j = `i'+1
	rename v`i' vv`j'
}

foreach i of numlist 2/11 {
	rename vv`i' v`i'
}	

foreach i of numlist 2/11 {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2
}
drop if coeff3 == .
keep year coeff* ste*
foreach i of numlist 2/11 {
	* generating upper and lower bound for CI
	gen upper`i' = coeff`i' + 1.96*ste`i' 
	gen lower`i' = coeff`i' - 1.96*ste`i' 
}

* reshape into long format
reshape long coeff ste lower upper, i(year) j(wagerange)
gen status=1
save $DIR\tdist_p10, replace

* generating space in between bars for better visulation in the figure 
* create a status = 4 to make more room between each wage range 
*gen vv=_N
set obs `=_N*2'
replace status = 4 if status==.
replace wagerange=2 if status==4
replace wagerange=wagerange[_n-1]+1 if status==4 & year[_n-1]==. & wagerange==2

*for deciles
drop if wagerange>11
sort wagerange status
gen rangestatus = _n 
sort rangestatus

replace rangestatus=rangestatus+1
set obs `=_N+1'
replace rangestatus = 1 if rangestatus==.
sort rangestatus

* setting graph locals
local ymin = -0.015
local ymax = 0.00
local ygap = 0.005

twoway (bar coeff rangestatus ,  bcolor(navy)) (rcap lower upper rangestatus , lcolor(navy) lpattern(shortdash)), ///
		xlab(2 "<p10" 4 "p10-20" 6 "p20-30" 8 "p30-40" 10 "p40-50" 12 "p50-60" 14 "p60-70" 16 "p70-80" 18 "p80-90" 20 "p90-100"   ,angle(45)) xtitle("Employee earnings quantile") ylab(`ymin'(`ygap')`ymax', grid glcolor(gs15)) legend(off) ///
		plotregion(color(white)) graphregion(color(white)) bgcolor(white) ysize(7) xsize(10) 
graph export $OUTPUT/fig5_panelA.`figext', replace
			
/* black and white version */
twoway (bar coeff rangestatus ,  bcolor(gs4)) (rcap lower upper rangestatus , lcolor(gs4) lpattern(shortdash)), ///
		xlab(2 "<p10" 4 "p10-20" 6 "p20-30" 8 "p30-40" 10 "p40-50" 12 "p50-60" 14 "p60-70" 16 "p70-80" 18 "p80-90" 20 "p90-100"   ,angle(45)) xtitle("Employee earnings quantile") ylab(`ymin'(`ygap')`ymax', grid glcolor(gs15)) legend(off) ///
		plotregion(color(white)) graphregion(color(white)) bgcolor(white) ysize(7) xsize(10) 
graph export $OUTPUT/fig5_panelA_bw.`figext', replace
			
		
		
*****************************
** Panel B
*****************************
* set the ownerhsare coeff 
local ownsh "-.019"
local ownse ".00648"
insheet using $DIR\figure5_panelB_distrib_pre.csv , clear

gen tt1112=1
ren *_iv *
reshape long net_sh, i(tt1112) j(cat)

replace cat=13 if cat==99
gen cat2 = cat*3
gen cat3 = cat2-1
drop tt1112

* collapse top decile
gen znet_sh = net_sh
gen vv = 0
replace vv = net_sh if cat>=10 & cat<=12
egen vv7=sum(vv)
replace znet_sh = vv7 if cat==10

drop if cat>=11 & cat<=12 
replace cat = 11 if cat==13
drop vv vv7

drop net_sh
ren znet_sh net_sh

sort cat 
tempfile tt
save `tt', replace

use $DIR\tdist_p10, clear
gen cat=_n

drop if cat==11
replace cat=11 if cat==12
sort cat 

drop status  wagerange
sort cat 
merge 1:1 cat using `tt'

replace coef=. if cat==11
gen new_sh = net_sh + coef 
gen vv = net_sh if cat==11
egen osh = max(vv)

*  post owner share
gen osh_post = osh+`ownsh'
* se for owner 
replace ste = `ownse' if cat==11

replace upper = `ownsh' + 1.96*ste  if cat==11
replace lower = `ownsh' - 1.96*ste  if cat==11
gen oup = osh+upper
gen olo = osh+lower

drop vv
egen vwtot=sum(new_sh) 
gen vwsh=new_sh/vwtot
gen dist = vwsh*(1-osh_post)
replace dist = osh_post if cat==11

gen cat4 = cat*3-2
replace cat2 = 33 if cat2==39
replace cat3 = 32 if cat3==38

set obs `=_N*2'
gen ss=cat==.
sort ss cat
by ss: gen ssn=_n
sort ssn ss
gen run=_n*2 
gen run2=run-1
gen run3=run-2

twoway  (bar net_sh cat3,  bcolor(cranberry)) (bar dist cat2,  bcolor(gs6)) ///
		(rcap olo oup cat2 if cat==11, lcolor(gs6) ), ///
		xlab(2 "<p10" 5 "p10-20" 8 "p20-30" 11 "p30-40" 14 "p40-50" 17 "p50-60" 20 "p60-70" 23 "p70-80" 26 "p80-90" 29 "p90-100" 32 "Owners" ,angle(45)) ///
		xtitle("Employee earnings quantile") ytitle("share of after-tax surplus") ylab(0(.1).35, grid glcolor(gs15)) ///
		legend(symxsize(*.5) region(lc(none) fc(none) ls(none)) keygap(*.25) colgap(*.25) region(lwidth(none)) size(small) order(1 "pre-reform" 2 "post reform") position(12) row(1) ring(0)) ///
		plotregion(color(white)) graphregion(color(white)) bgcolor(white) ysize(7) xsize(10) 
graph export $OUTPUT/fig5_panelB.`figext', replace

/* black and white version */
twoway  (bar net_sh cat3,  bcolor(gs3)) (bar dist cat2,  bcolor(gs7)) ///
		(rcap olo oup cat2 if cat==11, lcolor(gs7) ), ///
		xlab(2 "<p10" 5 "p10-20" 8 "p20-30" 11 "p30-40" 14 "p40-50" 17 "p50-60" 20 "p60-70" 23 "p70-80" 26 "p80-90" 29 "p90-100" 32 "Owners" ,angle(45)) ///
		xtitle("Employee earnings quantile") ytitle("share of after-tax surplus") ylab(0(.1).35, grid glcolor(gs15)) ///
		legend(symxsize(*.5) region(lc(none) fc(none) ls(none)) keygap(*.25) colgap(*.25) region(lwidth(none)) size(small) order(1 "pre-reform" 2 "post reform") position(12) row(1) ring(0)) ///
		plotregion(color(white)) graphregion(color(white)) bgcolor(white) ysize(7) xsize(10) 
graph export $OUTPUT/fig5_panelB_bw.`figext', replace

erase $DIR\tdist_p10.dta



********************************************************************************
********************************************************************************
********************************************************************************
**  Fig 6 - Heterogeneity
********************************************************************************
********************************************************************************
********************************************************************************
* See Excel Sheet - fig6_new.xls for figure 
*  created from input tables: 
*		figure6_nowners.csv
*		figure6_fsize.csv 
*		figure6_valadd.csv
*		figure6_revenue.csv 
*		figure6_profitmarg.csv
*		figure6_labshare.csv 



********************************************************************************
********************************************************************************
********************************************************************************
**  Figure 7 - Liability quartiles
********************************************************************************
********************************************************************************
********************************************************************************
*set number of specs in table 
local ss = 5
local sp = `ss'+1

* import csv file and select first column only 
import delimited "$DIR\figure7.csv", rowrange(4:27) colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )

	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

set obs `=_N+1'
replace year = 2012 if year == .
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

*save tempfile to append to other industries
gen cat="ag"
gen zero =0
keep if year==2016

reshape long coeff upper lower ste, i(year cat) j(spec)

sort cat spec 
by cat: gen quintile=_n-1
gen group=floor(_n*(1/6.01))

	label values quintile quintile
	label define quintile ///
	0   "Losses" ///
	1	"Q1" ///
	2	"Q2" ///
	3	"Q3" ///
	4	"Q4" 
	tab quintile
	
local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.02
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

graph twoway (rcap lower upper quintile if cat=="ag", /*lpattern(dash)*/  lcolor(`secolor1') mcolor(navy)) ///
				(scatter coeff quintile if cat=="ag", mcolor(`lcolor1')) , ///
			/*ytitle("{&beta},2012-2015")*/ xtitle("") legend(off) /*legend(label(2 "DD coefficients") order(2)) */ ///
			yline(0, lcolor(gs10) ) yscale(range(-.025 .00))  ylabel(-.03 (.01) 0)	xlabel(,valuelabel /*angle(30)*/ ) ///
			graphregion(fcolor(white) icolor(white) color(white))  plotregion(margin(medlarge)) 
graph export "$OUTPUT/fig7_panelA.`figext'", replace

/* black and white version */
local lcolor1 "gs4"
local secolor1 "gs8"
graph twoway (rcap lower upper quintile if cat=="ag", /*lpattern(dash)*/  lcolor(`secolor1') mcolor(navy)) ///
				(scatter coeff quintile if cat=="ag", mcolor(`lcolor1')) , ///
			/*ytitle("{&beta},2012-2015")*/ xtitle("") legend(off) /*legend(label(2 "DD coefficients") order(2)) */ ///
			yline(0, lcolor(gs10) ) yscale(range(-.025 .00))  ylabel(-.03 (.01) 0)	xlabel(,valuelabel /*angle(30)*/ ) ///
			graphregion(fcolor(white) icolor(white) color(white))  plotregion(margin(medlarge)) 
graph export "$OUTPUT/fig7_panelA_bw.`figext'", replace




********************************************************************************
********************************************************************************
********************************************************************************
**  Fig 8 - Main - ITT 
********************************************************************************
********************************************************************************
********************************************************************************
**************
**  Panel A - all
**************
*set number of specs in table 
local ss = 1
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure8_panelA.csv", rowrange(4:`sl') colrange(:`sp') clear 
	
* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	local i "2"
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "itt"
tempfile titt
save `titt', replace

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "1"
local spec1_1 = `zspec1_1'+1

local gtype "scatter"
**
* Event study figure 
**
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig8_panelA.`figext', replace

/* black and white version */
local lcolor1 "gs4"
local secolor1 "gs8"
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig8_panelA_bw.`figext', replace



**************
**  Panel B - top only
**************
*set number of specs in table 
local ss = 1
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure8_panelB.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1
gen tt = "itt"

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "1"
local spec1_1 = `zspec1_1'+1

local gtype "scatter"
**
* Event study figure *
**
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig8_panelB.`figext', replace 

/* black and white version */
local lcolor1 "gs4"
local secolor1 "gs8"
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig8_panelB_bw.`figext', replace 

			

*******************************************************
**  Main - first stage
*******************************************************
* set rows - if base 2012: sr = 27 & srr=1 ;  if base 2011,2012: sr = 25 & srr=2
local sr = 25
local ss = 1
local sp = `ss'+1
local srr = 2

import delimited "$DIR\figure8_panelC.csv", rowrange(4:`sr') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}

drop if year == .
keep year coeff* ste*

set obs `=_N+1'
replace year = 2012 if year == .
sort year 

gen vv=`srr'
if vv==2 {
set obs `=_N+1'
replace year = 2011 if year == .
sort year 
}
drop vv

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero = 0
gen pliab_tot = 41671
replace pliab_tot=0 if year<=2012

forval i=2/`sp' {
	gen zz = coeff`i' if year>2012
	egen mean`i'  = mean(zz)
	drop zz
	gen msh`i'=mean`i'/pliab_tot
}

local lcolor1 "navy"
local secolor1 "navy"
local lcolor2 "cranberry"
local secolor2 "cranberry"
local ymin = 00000
local ymax = 60000
local ygap = 20000
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2012
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "0"
local spec1_1 = 14
local spec1_2 = 2
local spec1_3 = 9
local spec1_4 = 11

* 26 31 35
local zspec = 1
local spec = `zspec'+1

local gtype "connected"   
local xvarmin = 2011
local zspec = 1
local spec = `zspec'+1

graph twoway (`gtype' pliab_tot `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor2') lpattern(dash) msymbol(none) ) ///
			(`gtype' mean`spec' `xvar'  if year>=2013 & year<=`xvarmax' ,  lcolor(`secolor1') lpattern(shortdash) msymbol(none) ) ///
			(`gtype' coeff`spec' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			 xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' 2017))  ytitle("")  xtitle("year") ///
			xline(2012.3, lcolor(black) lpattern(dash)) ylab(`ymin'(`ygap')`ymax', nogrid)   ///
			 legend(symxsize(*.5) region(lc(none) fc(none) ls(none)) keygap(*.25) colgap(*.25) region(lwidth(none)) size(small) ring(0) position(5) col(1) order(1 "predicted increase in liability" 3 "estimated increase in liability" 2 "mean of estimated increase" )) ///
	       graphregion(color(white)) bgcolor(white)  ///
           ysize(7) xsize(10) ///
			text(44400 2016.2 "$44,184", place(e) color(navy) size(small))  ///	
			text(41300 2016.2 "$41,671", place(e) color(sienna) size(small)) 	
graph export $OUTPUT/fig8_panelC.`figext', replace


/* black and white version */
local lcolor1 "gs3"
local secolor1 "gs3"
local lcolor2 "gs8"
local secolor2 "gs8"

graph twoway (`gtype' pliab_tot `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor2') lpattern(dash) msymbol(none) ) ///
			(`gtype' mean`spec' `xvar'  if year>=2013 & year<=`xvarmax' ,  lcolor(`secolor1') lpattern(shortdash) msymbol(none) ) ///
			(`gtype' coeff`spec' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			 xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' 2017))  ytitle("")  xtitle("year") ///
			xline(2012.3, lcolor(black) lpattern(dash)) ylab(`ymin'(`ygap')`ymax', nogrid)   ///
			 legend(symxsize(*.5) region(lc(none) fc(none) ls(none)) keygap(*.25) colgap(*.25) region(lwidth(none)) size(small) ring(0) position(5) col(1) order(1 "predicted increase in liability" 3 "estimated increase in liability" 2 "mean of estimated increase" )) ///
	       graphregion(color(white)) bgcolor(white)  ///
           ysize(7) xsize(10) ///
			text(44400 2016.2 "$44,184", place(e) color(`secolor1') size(small))  ///	
			text(41300 2016.2 "$41,671", place(e) color(`secolor2') size(small)) 	
graph export $OUTPUT/fig8_panelC_bw.`figext', replace









		